package com.itqf.dao.impl;

import com.itqf.dao.AccountDao;
import com.itqf.entity.Account;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.sql.ResultSet;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/9/29
 * @Time: 下午2:57
 */
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

//    private JdbcTemplate jdbcTemplate;

    @Override
    public Account findById(String cartNo) {
        Account account = new Account();
    //getJdbcTemplate    JdbcDaoSupport提供
        getJdbcTemplate().query
                ("select * from account where cardno=?",new RowMapper<Account>(){
                    @Override
                    public Account mapRow(ResultSet rs, int rowNum) {
                        try {
                            //This method should not call {@code next()} on
                            //	 * the ResultSet;
                                account.setCartNo(rs.getString("cardno"));
                                account.setAccount_balance(rs.getDouble("account_balance"));
                                System.out.println(rs.getString("cardno")+"--"+rs.getDouble("account_balance"));
                           // }
                            return  account;
                        }catch (Exception e){
                            e.printStackTrace();
                        }

                        return null;
                    }
                },cartNo);

        return account;
    }

    @Override
    public int update(Account account) {
        //
        return getJdbcTemplate().update
                ("update  account set account_balance=? where cardno=?",account.getAccount_balance(),account.getCartNo());
    }


}
